﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using Universum.Infrastructure.Configuration;
using System.Drawing;
using System.Security.Principal;

namespace Universum.Model.Services
{
	public class FileService : IFileService
	{

		public IPrincipal User { get; set; }

		

		public bool FileExists(string filePath)
		{
			return File.Exists(filePath);
		}

		public Stream GetStreamFor(string filePath)
		{
			var fromPath = to_complete_path( filePath );
			return new FileStream(fromPath, FileMode.Open, FileAccess.Read, FileShare.Delete|FileShare.ReadWrite);
		}

		private string to_complete_path(string filePath)
		{
			return Path.Combine(ConstantIdentifiers.RESOURCES_PATH,
				filePath);
		}
					

		public void WriteToDiskFrom(HttpPostedFileBase resourceFile, string toPath)
		{

			var full_path = to_complete_path( toPath);
			var directory_path = Path.GetDirectoryName(full_path);
			if (!Directory.Exists(directory_path))
				Directory.CreateDirectory(directory_path);
			if (!File.Exists(full_path)) File.Create(full_path).Close();
			resourceFile.SaveAs(full_path);
		}

		public void WriteDirectory(string newFolderName, string path)
		{
			var base_directory = GetUserDirectory();
			var directoryPath = Path.Combine(base_directory,
				Path.Combine(path, newFolderName));
			if (!Directory.Exists(directoryPath))
				Directory.CreateDirectory(directoryPath);

		}

		public bool DirectoryExists(string directoyPath)
		{
			return Directory.Exists(
				Path.Combine(GetUserDirectory(),
				directoyPath));
		}
				

		public IList<string> GetDirectoriesFor()
		{
			string basePath = GetUserDirectory();
			if (Directory.Exists(basePath) == false) 
				return new List<string>();
			return Directory.GetDirectories(basePath).ToList();
		}

		private string GetUserDirectory()
		{
			return Path.Combine(
				Universum.Infrastructure.Configuration.ConstantIdentifiers.RESOURCES_PATH,
				User.Identity.Name
				);
		}

		
		public IList<string> GetAllDirectoriesFor(string path)
		{
			string directory_path = to_configured_path(path);
			if (Directory.Exists(directory_path).IsFalse()) return new List<string>();

			List<string> directories = new List<string>();
			Directory.GetDirectories(directory_path).ToList().ForEach(
				directory_name =>
				{
					directories.Add(
						directory_name.Substring(directory_path.Length+1)
					);
				}
			);
			return directories;
		}

		private string to_configured_path(string path)
		{
			string base_directory = GetUserDirectory();
			string directory_path = Path.Combine(base_directory,
				path);
			return directory_path;
		}

		

	

		public IList<string> GetAllFilesFor(string path)
		{
			var from_path = to_configured_path(path);
			if (DirectoryExists(from_path))
			{
				return Directory.GetFiles(from_path).ToList();
			}
			return new List<string>();
		}

	

		


		public void DeleteFolder(string path)
		{
			var at_path = to_configured_path(path);
			Directory.Delete(at_path);

		}

		public void DeleteFile(string path)
		{
			var at_path = to_complete_path(path);
			File.Delete(at_path);
		}

		
	}
}
